if(!requestScreenCapture()){
    exit();
}
// sleep(1000)
// let y = id('buttonDesc').text('91.42k').findOne().bounds().centerY();
// let x = id('buttonDesc').text('91.42k').findOne().bounds().centerX();
// click(x,y)
// className('android.widget.ImageView').depth(5).click()
// shareReceive();
// log(className('android.widget.ImageView').depth(5).exists())
// id('tt_video_ad_close_layout').click()
// exit();
console.show();
let otherStop = false;
// 点击滑动的礼物
function clickScrollGift () {
    let giftImg = images.read('/storage/emulated/0/Pictures/QQ/gift(1).png');
    let screenImg = captureScreen()
    var point = findImage(screenImg, giftImg);
    
    if (point) {
        console.log('找到坐标' + point.x + '的礼物移动,进行礼物点击')
        click(point.x+50, point.y);
        sleep(500)
        watchConfirmTv();
    }
    
}
// 观看视屏
function watchConfirmTv(){
    // && 
    if (id ('confirmTv').findOne().text() === '确定') {
        id ('confirmTv').click();
    }
    if (id ('confirmTv').exists() ) { // 能白嫖不看视屏的时候才点击领取
        id ('confirmTv').click();
        otherStop = true;
        let isCloseAd = false;
        sleep(3000)
        
        while (!isCloseAd) {
            console.log('正在观看')
            // console.log(className('android.widget.ImageView').depth(5).exists())
            // console.log(id('tt_video_ad_close_layout').exists())
            if (className('android.widget.ImageView').depth(5).exists()) {
                className('android.widget.ImageView').click()
                isCloseAd = true
            }
            sleep(1000)
            if (id('tt_video_ad_close_layout').exists()) {
                id('tt_video_ad_close_layout').click()
                isCloseAd = true
            }
            sleep(1000)
        }
        sleep(1000)
        id('confirmTv').click()
        console.log('视频观看完毕')
        otherStop = false
    } 
}
// 金币是否足够
function goldEnough (callback) {
    let totalGoldText = id('tv_total_gold').findOne().text();
    totalGoldText = parseGold(totalGoldText);
    let carGoldText = id('tv_add_money').findOne().text();
    carGoldText = parseGold(carGoldText);
    callback(carGoldText, totalGoldText )
}
function parseGold (goldText) {
    let Company = goldText.substr(goldText.length-1,goldText.length);
    let totalGoldText = Company === 'm' ? Number(goldText.substr(0,goldText.length - 1)) * 1000 : goldText.substr(0,goldText.length - 1);
    return totalGoldText
}
// 观看广告
function watchTv () {
    // 是否首页
    if (text('福利任务').exists()) {
        let X = text('福利任务').bounds().centerX() ; 
        let Y = text('福利任务').bounds().centerY() ; 
        click(X,Y);
        sleep(500)
        if (id('vg_topbar_right').exists()) {
            X = id('vg_topbar_right').bounds().centerX() ; 
            Y = id('vg_topbar_right').bounds().centerY() ; 
            click(X,Y);
            sleep(100);


        }
    }
}
const closeThread = threads.start(closeBtn); // 开启一个线程循环判断是否有遮挡物出现
function closeBtn () {
    // 循环判断是否有遮挡物出现
    while(true){
        let closeEle = id('ivClose');
        // 判断是否有弹窗空间
        if (closeEle.exists()) {
            closeEle.click()
        }
        // 判断是否有礼物需要点击
        // clickScrollGift();
        
        sleep(6000)
        while(otherStop){
            sleep(2500)
        }
    }
    
}
// 线程购买车子
const bugCarThread = threads.start(buyCar);
function buyCar () {
    while(true){
        const spares = spareQuantity();
        addCar (spares);
        while(otherStop){
            sleep(1000);
        }
        sleep(5000);
    }
}
let shareNum = 0;
// 购买车子
function addCar (number) {
    for (let i=0; i<number; i++) {
        // 购买空余的车
        
        goldEnough(function(gold, totalGoldText){
            // 金币足够 直接购买贵的车
            console.log('当前金币' + totalGoldText + 'k, 购车需要:' + gold + 'k')
            if (parseInt(totalGoldText) >= parseInt(gold)) {
                console.log('金币足够买贵一点的车')
                let x = id('layout_car_add').findOne().bounds().centerX()
                let y = id('layout_car_add').findOne().bounds().centerY();
                click(x,y);
                sleep(200)
            } else {
                if (shareNum<2) {
                    console.log('看看弹出的框子是否金币很多');
                    let x = id('layout_car_add').findOne().bounds().centerX();
                    let y = id('layout_car_add').findOne().bounds().centerY();
                    otherStop = true
                    sleep(2000)
                    click(x,y);
                    sleep(200)
                    otherStop = false
                    let watchTvGold = id('coinTv').findOne().text();
                    watchTvGold = parseGold(watchTvGold);
                    if (parseInt(gold) < parseInt(watchTvGold) ) {
                        if ( id('confirmTv').findOne().text() === '看视频领取') {
                            if (id('footTv').findOne().text().indexOf('剩余0次') === -1) {
                                shareNum = 0;
                                console.log('开始观看视频')
                                watchConfirmTv();
                                
                                
                            }else{
                                console.log('观看视频次数不足')
                                shareNum = 2
                                id('closeIv').click()
                            }
                            
                        } else {
                            if ( id('confirmTv').findOne().text() === '分享领取') { 
                                shareReceive();
                            }
                        }
                        
    
                    }
                }else {
                    console.log('金币不足买便宜一点的车')
                    if (totalGoldText >=  91.42) {
                        console.log('大厅app金币已到达购买条件,正在购买')
                        // 金币不足 点击商店买便宜的车子
                        if (!id('titleTv').exists() || id('titleTv').findOne().text() !== '商店'){
                            id('iv_shop').click();
                        }
                        sleep(500);
                        let nowCargold = id('goldTv').findOne().text();
                        nowCargold = parseGold(nowCargold);
                        if (nowCargold >=  91.42) {
                            console.log('弹出框app金币已到达购买条件,正在购买')
                            let y = id('buttonDesc').text('91.42k').findOne().bounds().centerY();
                            let x = id('buttonDesc').text('91.42k').findOne().bounds().centerX();
                            click(x,y)
                            
                        } else {
                            console.log('弹出框app金币没有刷新,先关闭购买窗口')
                        }
                        sleep(300)
                        if (id('titleTv').exists() || id('titleTv').findOne().text() === '商店'){
                            id('iv_shop_close').click();
                        }
                        
                    }
                }
                
            }


        })
    }
    
}
// 分享领取
function shareReceive() {
    let element = id('confirmTv');
    log(element.findOne().text());
    if (element.exists() && element.findOne().text() === '分享领取') {
        console.log('开始分享领取')
        otherStop = true;
        element.click();
        sleep(1000)
        if (text('微信').exists()) {
            text('微信').findOne().parent().click();
            console.log('等待5秒返回')
            sleep(5000)
            back();
            sleep(1000)
            console.log('确定领取金币')
            id ('confirmTv').click();
            otherStop = false;
        }
    }
}
// 判断是否有16宫格空余几个
function spareQuantity () {
    const layouts = className('FrameLayout').depth(9).find();
    return 12  - (layouts.length - 1);
}
// // 合并车子
let lock = threads.lock(); // 线程锁
for(let i = 0; i< 3; i++ ){ //开启三个线程进行合成 基于多线程会导致多个线程滑动不灵敏
    threads.start(mergeCar); 
}
let gradeI = 0;
function mergeCar () {
    while(true){
        lock.lock(); // 这种写法 其实相当于单线程 因为把整个子程序都放进许可区了
        gradeI = ++ gradeI;
        if (gradeI > 38) {
            gradeI = 1;
        }
        // lock.unlock();
        let carsElements = id('tv_car_level').text(gradeI).find();
        if (carsElements.length > 1) {
            console.log('正在合成等级:' + gradeI + '的car')
            for (let carI = 0; carI<Math.floor(carsElements.length/2); carI++) {
                let currentIndex = 2 * carI ;
                let upperEleY = carsElements[currentIndex].bounds().centerY();
                let upperEleX = carsElements[currentIndex].bounds().centerX();
                let nextEleX = carsElements[currentIndex + 1].bounds().centerX();
                let nextEleY = carsElements[currentIndex + 1].bounds().centerY();
                // lock.lock();
                swipe(upperEleX, upperEleY, nextEleX, nextEleY, 200);
                
            }
        }
        lock.unlock();
        while(otherStop){
            sleep(2500);
        }
    }
}